//
//  YSY_Game_HerosViewController.m
//  GoodManNews
//
//  Created by dllo on 16/1/12.
//  Copyright © 2016年 dllo. All rights reserved.
//

#import "YSY_Game_HerosViewController.h"
#import "Masonry.h"
#import "YSY_Game_HeroDetailCollectionViewCell.h"
#import "GoodManNews_AFNetTool.h"
#import "YSY_Game_HeroModel.h"
#import "UIImageView+WebCache.h"
#import "YSY_Game_HeadCollectionReusableView.h"
#import "YSY_HeroDetail_ViewController.h"


#define URL @"http://lolbox.duowan.com/phone/apiHeroes.php?"
@interface YSY_Game_HerosViewController () <UICollectionViewDataSource, UICollectionViewDelegate, UISearchBarDelegate>
@property (nonatomic, retain) UICollectionView *heroCollectionView;
@property (nonatomic, retain) NSMutableArray *herosAllArr;         //所有英雄
@property (nonatomic, retain) NSMutableArray *herosArr;         //临时所有英雄
@property (nonatomic, retain) NSMutableArray *weekFreeHeroArr;  //周免英雄

@property (nonatomic, retain) MBProgressHUD *progressHUD;
@end

@implementation YSY_Game_HerosViewController
- (void)dealloc
{
    self.heroCollectionView.delegate = nil;
    self.heroCollectionView.dataSource = nil;
    [_progressHUD release];
    [_herosArr release];
    [_herosAllArr release];
    [_weekFreeHeroArr release];
    [_heroCollectionView release];
    [super dealloc];
}
- (void)viewWillAppear:(BOOL)animated {
    
    
}
- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = [UIColor whiteColor];
    [self createView];
    
    [self getData];
}
#pragma mark - 数据
- (void)getData {
    self.progressHUD = [[MBProgressHUD alloc] initWithView:self.view];
    [self.view addSubview:self.progressHUD];
    self.progressHUD.labelText = @"Loading";
    [self.progressHUD show:YES];
    [self.progressHUD release];
    
    [GoodManNews_AFNetTool getNetWithURL:URL body:nil headFile:nil responseStyle:WBNJSON success:^(NSURLSessionDataTask *task, id responseObject) {
        
        self.herosArr = [YSY_Game_HeroModel baseModelWithArr:responseObject[@"all"]];
        self.herosAllArr = [NSMutableArray arrayWithArray:self.herosArr];
        [YSY_HeroArr shareHeroArr].heroArr = [NSMutableArray arrayWithArray:self.herosArr];
        self.weekFreeHeroArr = [YSY_Game_HeroModel baseModelWithArr:responseObject[@"free"]];
        [self.heroCollectionView reloadData];
        
        [self.progressHUD hide:YES] ;
        [self.heroCollectionView.mj_header endRefreshing];
    } failure:^(NSURLSessionDataTask *task, NSError *error) {
        
        
    }];
    
}
#pragma mark - 私有方法
- (void)createView {
    //创建视图
    [self createCollectionView];
}

- (void)createCollectionView {
    UICollectionViewFlowLayout *flowlayout = [UICollectionViewFlowLayout new];
    //头大小
    flowlayout.headerReferenceSize = CGSizeMake(self.view.bounds.size.width, self.view.bounds.size.height / 15);
    flowlayout.itemSize = CGSizeMake(self.view.bounds.size.width / 4, self.view.bounds.size.height / 5);
    flowlayout.sectionInset = UIEdgeInsetsMake(10, 10, 10, 10);
    
    
    
    self.heroCollectionView = [[UICollectionView alloc] initWithFrame:CGRectZero collectionViewLayout:flowlayout];
    self.heroCollectionView.backgroundColor = [UIColor whiteColor];
//    self.heroCollectionView.contentOffset = CGPointMake(0, self.view.bounds.size.height / 15);
    [self.view addSubview:self.heroCollectionView];

    [self.heroCollectionView mas_makeConstraints:^(MASConstraintMaker *make) {
//        make.edges.equalTo(self.view);
        make.top.equalTo(self.view).with.offset(self.view.bounds.size.height / 15 + 10);
        make.left.right.and.bottom.equalTo(self.view);
        
    }];
    self.heroCollectionView.delegate = self;
    self.heroCollectionView.dataSource = self;
//下拉刷新
    self.heroCollectionView.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
        [self getData];
        [self.heroCollectionView.mj_header beginRefreshing];
    }];
    
//注册cell
    [self.heroCollectionView registerClass:[YSY_Game_HeroDetailCollectionViewCell class] forCellWithReuseIdentifier:@"cell"];
    
    [self.heroCollectionView registerClass:[YSY_Game_HeadCollectionReusableView class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"headViewCell"];
    
    
    
}

#pragma mark - UICollectionView协议方法
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    if (self.herosArr) {
        return self.herosArr.count;
    }else {
        return 0;
    }
    
}

//铺cell
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    YSY_Game_HeroDetailCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"cell" forIndexPath:indexPath];
    YSY_Game_HeroModel *heroM = self.herosArr[indexPath.row];
    cell.titleL.text = heroM.title;
    if (heroM.title.length > 5) {
        cell.titleL.font = [UIFont systemFontOfSize:13];
    }else {
        cell.titleL.font = [UIFont systemFontOfSize:17];
    }
    cell.cnNameL.text = heroM.cnName;
    if (heroM.cnName.length > 5) {
        cell.cnNameL.font = [UIFont systemFontOfSize:15];
    }else {
        cell.cnNameL.font = [UIFont systemFontOfSize:17];
    }
    NSString *headImageURL = [NSString stringWithFormat:@"http://img.lolbox.duowan.com/champions/%@_120x120.jpg", heroM.enName];
    [cell.headV sd_setImageWithURL:[NSURL URLWithString:headImageURL] placeholderImage:nil];
    return cell;
}

//头视图
- (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath {
    //kind 两种
    //UICollectionElementKindSectionFooter
    //UICollectionElementKindSectionHeader
    YSY_Game_HeadCollectionReusableView *headView = [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"headViewCell" forIndexPath:indexPath];
    //添加搜索
    UISearchBar *searchBar = [UISearchBar new];
    searchBar.showsCancelButton = YES;
    searchBar.placeholder = @"按英雄名字搜索";
    [headView addSubview:searchBar];
    [searchBar mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.equalTo(headView);
        make.edges.lessThanOrEqualTo(headView);
    }];
    searchBar.delegate = self;
    
    return headView;
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
   
    YSY_Game_HeroModel *model = self.herosArr[indexPath.row];
    [self.delegate jumpToHeroDetailPage:model.enName cnName:model.cnName];
    
}
#pragma mark -UISearchBar
//输入内容改变
- (void)searchBar:(UISearchBar *)searchBar textDidChange:(NSString *)searchText; {
    [self.herosArr removeAllObjects];
}
//开始编辑
- (BOOL)searchBarShouldBeginEditing:(UISearchBar *)searchBar; {
    
    return YES;
}


- (void)searchBarSearchButtonClicked:(UISearchBar *)searchBar {
    [searchBar resignFirstResponder];
    [self.herosArr removeAllObjects];
    for (YSY_Game_HeroModel *model in self.herosAllArr) {
        if ([model.title isEqualToString:searchBar.text]) {
            [self.herosArr addObject:model];
        }
        if ([model.cnName isEqualToString:searchBar.text]) {
            [self.herosArr addObject:model];
        }
        if ([model.enName isEqualToString:searchBar.text]) {
            [self.herosArr addObject:model];
        }
    }
    [self.heroCollectionView reloadData];
    
}

- (void)searchBarCancelButtonClicked:(UISearchBar *)searchBar {
    [searchBar resignFirstResponder];
    [self.herosArr removeAllObjects];
    [self.herosArr addObjectsFromArray:self.herosAllArr];
    [self.heroCollectionView reloadData];
}












- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
